package types

import (
	"go.uber.org/atomic"
)

const (
	// StatusUnknown 故障状态（端口冲突、依赖项未启动、启动失败等）
	StatusUnknown int32 = iota - 1

	// StatusActivated 激活状态
	StatusActivated

	// StatusSuspend 挂起状态
	StatusSuspend
)

// ConsensusStatus 模块状态
type ConsensusStatus struct {
	status atomic.Int32
}

// InitStatus 初始化共识状态
func (m *ConsensusStatus) InitStatus() {
	m.status.Store(StatusUnknown)
}

// ConsensusSuspend 共识挂起
func (m *ConsensusStatus) ConsensusSuspend() error {
	m.status.Store(StatusSuspend)
	return nil
}

// ConsensusActive 共识激活
func (m *ConsensusStatus) ConsensusActive() error {
	m.status.Store(StatusActivated)
	return nil
}

// IsActivited 共识是否激活
func (m *ConsensusStatus) IsActivited() bool {
	return m.status.Load() == StatusActivated
}

// CStatus 共识状态获取
func (m *ConsensusStatus) CStatus() int32 {
	return m.status.Load()
}
